package com.chrray.position.service.upload.impl;


import com.chrray.position.pojo.vo.util.UploadVo;
import com.chrray.position.service.upload.UploadrService;
import com.chrray.position.util.file.UploadFileUtil;

import com.chrray.position.util.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

import static com.chrray.position.util.result.ResponseResults.fail;
import static com.chrray.position.util.result.ResponseResults.success;


/**
 * 上传文件
 *
 * @author li'miao
 */
@Service
@Slf4j
public class UploadServiceImpl implements UploadrService {


    @Value("${uploadFile.filePath}")
    String filePath;
    @Value("${uploadFile.imgPath}")
    String imgPath;

    @Value("${uploadFile.visitUrl}")
    String visitUrl;





    @Override
    public ResponseResult uploadPic(MultipartFile upload) {
        // 上传图片
        UploadFileUtil uploadFileUtil = new UploadFileUtil();
        String url = uploadFileUtil.uploadFile(upload, filePath, imgPath);
        String path = url.substring(url.indexOf(imgPath));
        UploadVo uploadVo = new UploadVo();
        uploadVo.setUrl(path);
        uploadVo.setFullPathUrl(visitUrl + path);
        return success(uploadVo);
    }

    @Override
    public InputStream getImg(String path) {
        try {
            return Files.newInputStream(Paths.get(filePath + path));
        } catch (IOException e) {
            log.error("文件不存在", e);
        }
        return null;
    }




}
